#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/XKBlib.h>
#include <stdio.h>
#include <err.h>

// macros
#define POSX   (1920 - WIDTH)/2
#define POSY   (1080 - HEIGHT)/2
#define WIDTH  800
#define HEIGHT 800
#define BORDER 12

// Variables
static Display *dpy; // display server
static int scr;      // screen
static Window root;  // root window

static Window
createwin(int x, int y, int w, int h, int b)
{
  Window win;
  XSetWindowAttributes xwa; // the attributes for window

  xwa.background_pixel = WhitePixel(dpy, scr);
  xwa.border_pixel = BlackPixel(dpy, scr);
  xwa.event_mask = ButtonPress;

  win = XCreateWindow(dpy, root, x, y, w, h, b, DefaultDepth(dpy, scr), InputOutput, DefaultVisual(dpy, scr),
                      CWBackPixel | CWBorderPixel | CWEventMask, &xwa);
  return win;
}

static void
run()
{
  XEvent ev;

  while (XNextEvent(dpy, &ev) == 0) {
    switch (ev.type) {
      case ButtonPress:
        return;
    }
  }
}

int main()
{
  Window win; // our window

  if ((dpy = XOpenDisplay(NULL)) == NULL) {
    errx(1, "Can't open display");
  }

  scr = DefaultScreen(dpy);
  root = RootWindow(dpy, scr);

  win = createwin(POSX, POSY, WIDTH, HEIGHT, BORDER);

  XMapWindow(dpy, win);

  run();

  // Cleanup
  XUnmapWindow(dpy, win);
  XDestroyWindow(dpy, win);
  XCloseDisplay(dpy);

  return 0;
}
